<?php
namespace Admin\Controller;
use Think\Controller;
class ArticleController extends Controller {
    //TP构造函数
    public function _initialize(){
        //自动加载的类
        new \Org\Admin_autoload();
    }
    //文章列表
    public function index(){
        $data['title']='文章管理 文章 - 找金银网';
        if(I('path.2')!=''){
            $page=I('path.2');
        }else{
            $page=1;
        }
        $perpage=10;
        $Model=D("Article");
        //搜索 关键词
        if(I('post.keyword')){
            $Model->keyword=I('post.keyword');
        }
        //搜索 分类
        if(I('post.ac_id')){
            $Model->ac_id=I('post.ac_id');
        }
        $Model->page=$page;
        $Model->perpage=$perpage;
        $article_list=$Model->get_article_list();
        $article_total=$Model->get_article_total();
        $page_total=ceil($article_total/$perpage);
        //分页数据
        if($page!=1&&$page_total>1){
            $last_page=$page-1;
        }else{
            $last_page=1;
        }
        if($page_total>$page){
            $next_page=$page+1;
        }else{
            $next_page=$page;
        }
        $pager['current_page']=$page;
        $pager['perpage']=$perpage;
        $pager['last_page']=$last_page;
        $pager['next_page']=$next_page;
        $pager['article_total']=$article_total;
        $pager['page_total']=$page_total;
        $Model=D("ArticleClass");
        $cat_list=$Model->get_cat_list();
        $this->assign("article_list", $article_list);
        $this->assign("cat_list", $cat_list);
        $this->assign("pager",$pager);
        $this->assign("data", $data);
        $this->display();
    }
    //添加文章
    public function article_add(){
        $data['title']='添加文章 文章 - 找金银网';
        $Model=D("ArticleClass");
        $cat_list=$Model->get_cat_list();
        $this->assign("data", $data);
        $this->assign("cat_list", $cat_list);
        $this->display();
    }
    //
    public function article_add_post(){
        if($_POST['article_title']!=''&&$_POST['article_content']!=''){
            $post=I('post.');
            $post['article_content']=I('post.article_content','','');
            $post['article_time']=time();
            $post['article_edit_time']=time();
            //print_r($post);exit;
            $Model=D("Article");
            $Model->post=$post;
            if(!$Model->article_add_post()){
                echo "<script>alert('提交失败，请您尝试重新提交');history.back(-1);</script>";exit;
            }
            echo "<script>alert('提交成功');window.location.href='/admin/article/article_add';</script>";
        }
    }
    //添加文章
    public function article_edit(){
        $data['title']='编辑文章 文章 - 找金银网';
        $article_id=I('path.2');
        $Model=D("Article");
        $Model->article_id=$article_id;
        $article_info=$Model->get_article_info();
        $Model=D("ArticleClass");
        $cat_list=$Model->get_cat_list();
        $this->assign("data", $data);
        $this->assign("article_info", $article_info);
        $this->assign("cat_list", $cat_list);
        $this->display();
    }
    //
    public function article_edit_post(){
        if($_POST['article_title']!=''&&$_POST['article_content']!=''){
            $post=I('post.');
            $post['article_content']=I('post.article_content','','');
            $post['article_edit_time']=time();
            //print_r($post);exit;
            $Model=D("Article");
            $Model->post=$post;
            if(!$Model->article_edit_post()){
                echo "<script>alert('提交失败，请您尝试重新提交');history.back(-1);</script>";exit;
            }
            echo "<script>alert('提交成功');window.location.href='/admin/article/index';</script>";
        }
    }
    //删除
    public function update_article()
    {
        if(I('post.ids')){
            $ids=I('post.ids');
            $Model=D("Article");
            $Model->ids=$ids;
            if(!$Model->delete_article()){
                echo "<script>alert('删除失败，请您尝试重新提交');history.back(-1);</script>";exit;
            }
            echo "<script>self.location=document.referrer;</script>";exit;
        }elseif(I('post.article_ids')){
            $ids=I('post.article_ids');
            $sorts=I('post.sorts');
            $Model=D("Article");
            $Model->ids=$ids;
            $Model->sorts=$sorts;
            if(!$Model->update_article_sort()){
                echo "<script>alert('更新失败，请您尝试重新提交');history.back(-1);</script>";exit;
            }
            echo "<script>self.location=document.referrer;</script>";
        }
    }
    //文章类别
    public function category(){
        $Model=D("ArticleClass");
        $cat_list=$Model->get_cat_list();
        //print_r($cat_list);exit;
        $data['title']='文章类别 - 找金银网';
        $this->assign("data", $data);
        $this->assign("cat_list", $cat_list);
        $this->display();
    }
    //
    public function cat_edit(){
        $data['title']='编辑文章分类 文章 - 找金银网';
        $ac_id=I('path.2');
        $Model=D("ArticleClass");
        $Model->ac_id=$ac_id;
        $info=$Model->get_ac_info();
        //print_r($info);exit;
        $this->assign("data", $data);
        $this->assign("info", $info);
        $this->display();
    }
    //
    public function cat_edit_post(){
        $post=I('post.');
        //print_r($post);exit;
        $Model=D("ArticleClass");
        $Model->post=$post;
        if(!$Model->cat_edit_post()){
            echo "<script>alert('写入数据库失败，请您重新提交');history.back(-1);</script>";exit;
        }
        echo "<script>alert('提交成功');window.location.href='/admin/article/category';</script>";
    }
    //
    public function cat_add(){
        $data['title']='添加文章分类 文章 - 找金银网';
        $this->assign("data", $data);
        $this->display();
    }
    //
    public function cat_add_post(){
        $post=I('post.');
        $Model=D("ArticleClass");
        $Model->post=$post;
        if(!$Model->cat_add_post()){
            echo "<script>alert('写入数据库失败，请您重新提交');history.back(-1);</script>";exit;
        }
        echo "<script>alert('提交成功');window.location.href='/admin/article/category';</script>";
    }
    //删除
    public function update_cat()
    {
        if(I('post.ids')){ //删除
            $ids=I('post.ids');
            $Model=D("ArticleClass");
            $Model->ids=$ids;
            if(!$Model->delete_cat()){
                echo "<script>alert('删除失败，请您尝试重新提交');history.back(-1);</script>";exit;
            }
            echo "<script>self.location=document.referrer;</script>";exit;
        }elseif(I('post.ac_ids')){  //更新
            $ids=I('post.ac_ids');
            $sorts=I('post.sorts');
            $Model=D("ArticleClass");
            $Model->ids=$ids;
            $Model->sorts=$sorts;
            if(!$Model->update_cat_sort()){
                echo "<script>alert('更新失败，请您尝试重新提交');history.back(-1);</script>";exit;
            }
            echo "<script>self.location=document.referrer;</script>";
        }
    }

    //系统文章 共用article表
    public function system_article(){
        $data['title']='系统文章 文章 - 找金银网';
        if(I('path.2')!=''){
            $page=I('path.2');
        }else{
            $page=1;
        }
        $perpage=10;
        $Model=D("Article");
        //搜索 关键词
        if(I('post.keyword')){
            $Model->keyword=I('post.keyword');
        }
        //搜索 分类
        if(I('post.ac_id')){
            $Model->ac_id=I('post.ac_id');
        }
        $Model->page=$page;
        $Model->perpage=$perpage;
        $article_list=$Model->get_system_article_list();
        //print_r($article_list);exit;
        $article_total=$Model->get_system_article_total();
        $page_total=ceil($article_total/$perpage);
        //分页数据
        if($page!=1&&$page_total>1){
            $last_page=$page-1;
        }else{
            $last_page=1;
        }
        if($page_total>$page){
            $next_page=$page+1;
        }else{
            $next_page=$page;
        }
        $pager['current_page']=$page;
        $pager['perpage']=$perpage;
        $pager['last_page']=$last_page;
        $pager['next_page']=$next_page;
        $pager['article_total']=$article_total;
        $pager['page_total']=$page_total;
        $Model=D("ArticleClass");
        $cat_list=$Model->get_system_cat_list();
        $this->assign("article_list", $article_list);
        $this->assign("cat_list", $cat_list);
        $this->assign("pager",$pager);
        $this->assign("data", $data);
        $this->display();
    }
    //系统文章类别
    public function system_cat(){
        $Model=D("ArticleClass");
        $cat_list=$Model->get_system_cat_list();
        //print_r($cat_list);exit;
        $data['title']='系统文章类别 文章 - 找金银网';
        $this->assign("data", $data);
        $this->assign("cat_list", $cat_list);
        $this->display();
    }
    //
    public function system_cat_add(){
        $Model=D("ArticleClass");
        $parent_id_list=$Model->get_system_cat_list();
        $data['title']='添加文章分类 文章 - 找金银网';
        $this->assign("data", $data);
        $this->assign("parent_id_list", $parent_id_list);
        $this->display();
    }
    //
    public function system_cat_add_post(){
        $post=I('post.');
        $Model=D("ArticleClass");
        $Model->post=$post;
        if(!$Model->system_cat_add_post()){
            echo "<script>alert('写入数据库失败，请您重新提交');history.back(-1);</script>";exit;
        }
        echo "<script>alert('提交成功');window.location.href='/admin/article/system_cat_add';</script>";
    }
    //添加文章
    public function system_article_add(){
        $data['title']='添加系统文章 文章 - 找金银网';
        $Model=D("ArticleClass");
        $cat_list=$Model->get_system_cat_list();
        $this->assign("data", $data);
        $this->assign("cat_list", $cat_list);
        $this->display();
    }
    //
    public function system_article_add_post(){
        if($_POST['article_title']!=''&&$_POST['article_content']!=''){
            $post=I('post.');
            $post['article_content']=I('post.article_content','','');
            $post['article_time']=time();
            $post['article_edit_time']=time();
            //print_r($post);exit;
            $Model=D("Article");
            $Model->post=$post;
            if(!$Model->system_article_add_post()){
                echo "<script>alert('提交失败，请您尝试重新提交');history.back(-1);</script>";exit;
            }
            echo "<script>alert('提交成功');window.location.href='/admin/article/system_article_add';</script>";
        }
    }
    //
    public function system_article_edit(){
        $data['title']='编辑系统文章 文章 - 找金银网';
        $article_id=I('path.2');
        $Model=D("Article");
        $Model->article_id=$article_id;
        $article_info=$Model->get_article_info();
        $Model=D("ArticleClass");
        $cat_list=$Model->get_system_cat_list();
        $this->assign("data", $data);
        $this->assign("article_info", $article_info);
        $this->assign("cat_list", $cat_list);
        $this->display();
    }
    //
    public function system_article_edit_post(){
        if($_POST['article_title']!=''&&$_POST['article_content']!=''){
            $post=I('post.');
            $post['article_content']=I('post.article_content','','');
            $post['article_edit_time']=time();
            //print_r($post);exit;
            $Model=D("Article");
            $Model->post=$post;
            if(!$Model->article_edit_post()){
                echo "<script>alert('提交失败，请您尝试重新提交');history.back(-1);</script>";exit;
            }
            echo "<script>alert('提交成功');window.location.href='/admin/article/system_article';</script>";
        }
    }
    //更新排序及批量删除
    public function update_system_cat()
    {
        if(I('post.ids')){
            $ids=I('post.ids');
            $Model=D("ArticleClass");
            $Model->ids=$ids;
            if(!$Model->delete_cat()){
                echo "<script>alert('删除失败，请您尝试重新提交');history.back(-1);</script>";exit;
            }
            echo "<script>self.location=document.referrer;</script>";exit;
        }elseif(I('post.ac_ids')){
            $ids=I('post.ac_ids');
            $sorts=I('post.sorts');
            //print_r($sorts);exit;
            $Model=D("ArticleClass");
            $Model->ids=$ids;
            $Model->sorts=$sorts;
            if(!$Model->update_cat_sort()){
                echo "<script>alert('更新失败，请您尝试重新提交');history.back(-1);</script>";exit;
            }
            echo "<script>self.location=document.referrer;</script>";
        }
    }
    //
    public function system_cat_edit(){
        $data['title']='编辑系统文章分类 文章 - 找金银网';
        $ac_id=I('path.2');
        $Model=D("ArticleClass");
        $Model->ac_id=$ac_id;
        $info=$Model->get_ac_info();
        $cat_list=$Model->get_system_cat_list();
        //print_r($info);exit;
        $this->assign("data", $data);
        $this->assign("info", $info);
        $this->assign("cat_list", $cat_list);
        $this->display();
    }
    //
    public function system_cat_edit_post(){
        $post=I('post.');
        //print_r($post);exit;
        $Model=D("ArticleClass");
        $Model->post=$post;
        if(!$Model->cat_edit_post()){
            echo "<script>alert('写入数据库失败，请您重新提交');history.back(-1);</script>";exit;
        }
        echo "<script>alert('提交成功');window.location.href='/admin/article/system_cat';</script>";
    }

}